local json = require("json")
local http = require("simplehttp")
http.TIMEOUT = 5

-- 口吐芬芳 url
local penRen = 'https://api.lolimi.cn/API/kout/k.php'
-- 神回复 url
local duanzi ="https://api.jixs.cc/api/wenan-duanzi/index.php"
-- 毒鸡汤 url
local dujitang ="https://api.wer.plus/api/djt"

local P = {}
function P.func(key, env)
    local engine     = env.engine
    local config     = engine.schema.config
    local first_key = config:get_string("custom_key/first")
    local second_key = config:get_string("custom_key/second")
    
    local third_key = config:get_string("custom_key/third")
    if (key:repr() == first_key) then
        local reply = http.request(penRen)
        if reply==nil or reply=='' then
            return 1
        end
        engine:commit_text(reply)
    end
  
    if (key:repr() == second_key) then
        local reply = http.request(duanzi)
        if reply==nil or reply=='' then
            return 1
        end
        local _, j = pcall(json.decode, reply)
        if j.code == "获取段子成功" then
            engine:commit_text(j.data.duanzi)
            return 2
        end
    end

    if (key:repr() == third_key) then
        local reply = http.request(dujitang)
        if reply==nil or reply=='' then
            return 1
        end
        local _, j = pcall(json.decode, reply)
        if j.code == 200 then
            engine:commit_text(j.data.comment)
            return 2
        end
    end
    return 2 -- kNoop
end

http_chars = P
